import { 
    likePost, 
    unlikePost, 
    fetchPostLikes,
    likeComment,
    unlikeComment 
  } from '@/api/like'
  
  const state = {
    postLikes: {}, // 格式: { postId: [like1, like2] }
    loading: false,
    error: null
  }
  
  const getters = {
    hasLikedPost: (state) => (postId, userId) => {
      return state.postLikes[postId]?.some(like => like.userId === userId) || false
    },
    postLikesCount: (state) => (postId) => {
      return state.postLikes[postId]?.length || 0
    }
  }
  
  const mutations = {
    ADD_POST_LIKE(state, { postId, like }) {
      if (!state.postLikes[postId]) {
        state.postLikes[postId] = []
      }
      state.postLikes[postId].push(like)
    },
    REMOVE_POST_LIKE(state, { postId, userId }) {
      if (state.postLikes[postId]) {
        state.postLikes[postId] = state.postLikes[postId].filter(like => like.userId !== userId)
      }
    },
    SET_POST_LIKES(state, { postId, likes }) {
      state.postLikes[postId] = likes
    },
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_ERROR(state, error) {
      state.error = error
    }
  }
  
  const actions = {
    async likePost({ commit, rootState }, postId) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const response = await likePost(postId)
        const userId = rootState.auth.user.id
        commit('ADD_POST_LIKE', { postId, like: { userId } })
        return response
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async unlikePost({ commit, rootState }, postId) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        await unlikePost(postId)
        const userId = rootState.auth.user.id
        commit('REMOVE_POST_LIKE', { postId, userId })
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async fetchPostLikes({ commit }, { postId, params }) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const likes = await fetchPostLikes(postId, params)
        commit('SET_POST_LIKES', { postId, likes })
        return likes
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async likeComment({ commit }, commentId) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        return await likeComment(commentId)
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async unlikeComment({ commit }, commentId) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        return await unlikeComment(commentId)
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    }
  }
  
  export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
  }